<?php
//表单模型
class Form {

	/**
	 * 实例化
	 * @return my
	 */
	public static function getInstance(){
		$form = new Form();
		return $form;
	}

	/**
	 * 分类id -> 自定义表单数据
	 * @param string $id 分类id(如果当前分类有表单模型数据则直接取,否则如果顶级分类有就取顶级分类,,否则全没有的话就退出) 
	 */
	public static function catId2FORM($id=0){
		if(!My::is_id($id))
			return false;
		$topParentId = M('admin.category')->id2topParent($id);
		$verify_id = 0;
		$form_model = C('form_model');
		if(isset($form_model[$id]))
			$verify_id = $id;
		elseif(isset($form_model[$topParentId['id']]))
			$verify_id = $topParentId['id'];
		//不存在verify_id时 返回
		if(!My::is_id($verify_id)) return false;
		//分类id -> 自定义表单数据
		$result = Form::id2list($verify_id);
		return $result;
	}
	/**
	 * 分类的唯一名称 -> 相应的自定义表单
	 * @param $item 分类的唯一名称
	 */
	public static function item2FORM($item=''){
		if($item=='' OR !My::is_var($item)) return false;
		$id = M('admin.category')->item2id($item);
		if(!My::is_id($id)) return false;
		$result = Form::catid2FORM($id);
		return $result;
	}

	/**
	 * 分类id -> 自定义表单数据
	 * @param string $id 分类id
	 */
	private static function id2list($id=0){
		if(!My::is_id($id))
			return false;
		if(!$result=My::getCache('id2list'.$id,array('pre'=>'form'))){
			//从form表中获取相应的自定义表单项
			$list = DB('form')->where("cat_id='{$id}' AND publish=1 AND is_custom=1")->order("ordering ASC,id ASC")->select();
			if(is_array($list))
			foreach($list as $value){
				$value['params'] = My::decodeJson($value['params']);
				//$value['verify_rule'] = My::decodeJson($value['verify_rule']);
				$result[$value['field_name']] = $value;
			}
			My::setCache('id2list'.$id, $result, array('pre'=>'form'));
		}
		//print_r($result);
		return $result;
	}

	/**
	 * 表单控件类型
	 * 决定控件html函数:function form()
	 * 注意:key取名时,前面部分一定要跟控件名相符(如input_map则为input控件)->用于表单验证(不需要验证的如img可以不按此规则)
	 */
	public static function getType(){
		return array(
						'input' => '(input)文本框',
						'input_date' => '(date)日期控件',
						'input_color' => '(color)颜色控件',
						'input_array' => '(input[])动态文本框',
						'input_array2' => '(input[][])动态文本框2',
						'html' => 'html代码',
						'more' => '混合自定义',
						'html_api' => 'API(HTML代码)',
						'radio' => '(radio)单项选择',//bool/自定义
						'checkbox' => '(checkbox)复选框',//自定义项
						'select' => '(select)下拉选择',
						'select_db' => '(select)数据表下拉',
						'select_fun' => '(select)下拉<函数接口>',
						'textarea' => '(textarea)多行文本',
						'textarea_ueditor' => '(ueditor)编辑器',
						'textarea_xheditor' => '(xheditor)编辑器',
						'img' => '(img)单个图片',
						'img_array' => '(img[])动态图片',
						'img_array2' => '(img[])动态图片+单个input',
						'img_array3' => '(img[])动态图片+两个input',
						'img_array4' => '自定义',
						'input_map' => '(baidu)百度地图',
						'input_autocomplete' => '(autocomplete)自动完成',//自动完成
						'package' => '(package)自定义插件包',
						'file' => '(file)附件上传',
						'file_array' => '(file[])附件上传',
					);
	}
	/**
	 * 表单验证类型
	 */
	public static function getVerifyType(){
		return array(
			'required'=>	array('message'=>'不能为空','label'=>'必填', 'function'=>'isRequired'),
			'number'=>		array('message'=>'不是一个数字','label'=>'数字', 'function'=>'isNumber'),//^-?([1-9]\d*\.\d*[1-9]\d*|0\.\d*[1-9]\d*|\d+)$
			'number_int'=>	array('message'=>'不是一个整形数字','label'=>'数字(整数)', 'function'=>'isNumberInt'), //\d+
			'number_float'=>array('message'=>'不是一个浮点数字','label'=>'数字(浮点数)', 'function'=>'isNumberFloat'), //^(-?\d+)(\.\d+)?$
			'letter'=>		array('message'=>'不是字母','label'=>'字母', 'function'=>'isLetter'), //[a-zA-Z]+
			'letter_number'=>array('message'=>'不是数字+字母','label'=>'数字+字母', 'function'=>'isLetterNumber'), //[0-9a-zA-Z]+
			'email'=>		array('message'=>'不是E-mail地址','label'=>'E-mail', 'function'=>'isEmail'), //\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
			'qq'=>			array('message'=>'不是一个QQ号码','label'=>'QQ', 'function'=>'isQQ'), //[1-9][0-9]{4,}
			'url'=>			array('message'=>'不是一个超级链接','label'=>'超级链接', 'function'=>'isUrl'), //[a-zA-z]+://[^\s]*
			'mobile'=>		array('message'=>'不正确','label'=>'手机号码', 'function'=>'isTelNumber'), //^(1)[0-9]{10}$
			'tel'=>			array('message'=>'不是电话号码','label'=>'电话号码', 'function'=>'isTelPhone'), //\d{3}-\d{8}|\d{4}-\{7,8}
			'Postal'=>		array('message'=>'不是邮政编码','label'=>'邮政编码', 'function'=>'isZipcode'), //[1-9]\d{5}(?!\d)
			'idcard'=>		array('message'=>'不是身份证','label'=>'身份证', 'function'=>'isIdCard'), //^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$
			'custom'=>		array('message'=>'格式不正确','label'=>'自定义正则', 'function'=>'custom'),
		);
	}
	/**
	 * 表单模型->返回相应控件的html
	 * @param string $params[name] 控件名称(name和fullName可选二选一)<$name有值时默认要另外添加上params[$name]>
	 * @param string $params[fullName] 全名(name和fullName可选二选一)<$fullName有值时不用加上params前缀,直接作为控件名>
	 * @param string $params[type] 控件类型
	 * @param array $params[value] 整个表单模型的数组
	 * @param array $params[params] 额外的params参数
	 * @return html
	 */
	public static function form_form($params){
		//echo "<pre>";print_r($params);exit;
		//表单控件类型数组
		$types = array_flip(Form::getType());
		if(!in_array($params['type'],$types))
			return "找不到控件类型({$params['type']})";
		$html = Widget::$params['type']($params);
		return $html;
	}
		
	/**
	 * 表单验证设置项,得到tr一行的html
	 * 所属控制器 form
	 * @param int $params['id'] 来自smarty模板函数的参数 <id和name二选1,id优先级高>
	 * @param string $params['field_name']  来自ajax传递过来的参数(checkbox控件点击时促发) <id和name二选1,id优先级高> <name和cat_id两参数是一起的>
	 * @param string $params['cat_id']  分类id(表单验证单元) <name和cat_id两参数是一起的>
	 */
	public static function verify_detail_tr($params='') {
		$id = isset($params['id'])?(int)$params['id']:0;//verify表中的id
		$field_name = isset($params['field_name'])?$params['field_name']:'';
		$cat_id = isset($params['cat_id'])?(int)$params['cat_id']:0;
		if(!$id AND (!$field_name OR !My::is_id($cat_id))) return false;
		if(My::is_id($id)) $result = DB('form')->where("id={$id}")->find();
		else $result = DB('form')->where("field_name='{$field_name}' AND cat_id={$cat_id}")->find();
		//id
		$id = isset($result['id'])?$result['id']:'';
		//字段名
		$field_name = isset($result['field_name'])?$result['field_name']:$field_name;
		//控件类型
		$widget_type = Form::getType();
		$widget_type_html = "<select name='param[widget_type]' lang='{$id}' class='widget_type_change' style='width:257px'><option value=''>请选择...</option>";
		foreach($widget_type as $key=>$value){ $widget_type_html.="<option ".((!empty($result['widget_type']) AND $key==$result['widget_type'])?'selected="selected"':'')." value='{$key}'>{$value}</option>"; }
		$widget_type_html .= "</select>";
		//验证类型
		$getVerifyType = Form::getVerifyType();
		//控件类型表单验证规则(反序列化成数组)
		$result['verify_rule'] = My::decodeJson(isset($result['verify_rule'])?$result['verify_rule']:'');
		//解释params(当添加时它是不存在的)
		$result['params'] = My::decodeJson(isset($result['params'])?$result['params']:'');
		$verify_type_html = "<select class='chosen' data-placeholder='请选择验证类型...' multiple='multiple' style='width:236px;' name='param[verify_rule][]'>";
		foreach($getVerifyType as $key=>$value){ $verify_type_html.="<option ".((!empty($result['verify_rule']) AND in_array($key,$result['verify_rule']))?'selected="selected"':'')." value='{$key}'>{$value['label']}</option>"; }
		$verify_type_html .= "</select>";
		return "<input type='hidden' name='param[id]' value='{$id}'/>
				<input type='hidden' name='param[field_name]' value='{$field_name}'/>
				<table class='change_table margin0a'>
					<tbody class='ui-sortable'>
						<tr>
							<td class='textar'> 标题：</td>
							<td> <input style='width:250px' type='text' name='param[title]' value='".(My::arr2var($result,'title'))."' /> </td>
						</tr>
						<tr>
							<td class='textar'> 控件类型：</td>
							<td> {$widget_type_html} </td>
						</tr>
						<tr class='widget_type_tr' style='display:none;'>
							<td class='textar'>控件属性:</td>
							<td class='widget_type_td'></td>
						</tr>
						<tr>
							<td class='textar'> 验证规则：</td>
							<td> {$verify_type_html} </td>
						</tr>
						<tr>
							<td class='textar'> 是否发布：</td>
							<td>
								<label class='radio inline'>是<input type='radio' ".((isset($result['publish']) AND $result['publish']==1)?'checked="checked"':'')." name='param[publish]' value='1' /></label>　
								<label class='radio inline'>否<input type='radio' ".(empty($result['publish'])?'checked="checked"':'')." name='param[publish]' value='0' /></label>
							</td>
						</tr>
						<tr>
							<td class='textar'> 表单属性：</td>
							<td>
								<span class='inline radio'>白名单：</span>
								<label class='radio inline'>是<input type='radio' ".((isset($result['params']['white']) AND $result['params']['white']==1)?'checked="checked"':'')." name='param[params][white]' value='1' /></label>　
								<label class='radio inline'>否<input type='radio' ".(empty($result['params']['white'])?'checked="checked"':'')." name='param[params][white]' value='0' /></label>
								<br>
								<span class='inline radio'>索　引：</span>
								<label class='radio inline'>是<input type='radio' ".((isset($result['params']['indexing']) AND $result['params']['indexing']==1)?'checked="checked"':'')." name='param[params][indexing]' value='1' /></label>　
								<label class='radio inline'>否<input type='radio' ".(empty($result['params']['indexing'])?'checked="checked"':'')." name='param[params][indexing]' value='0' /></label>
							</td>
						</tr>
						<tr>
							<td class='textar'> 描述：</td>
							<td> <textarea style='width:245px;height:40px;' name='param[description]'>".(My::arr2var($result,'description'))."</textarea> </td>
						</tr>
					</tbody>
				</table>";
	}
	
	/**
	 * 表单模型参数设置->控件类型onchange时->得到相应的控件相关附加参数
	 * @param string $type 控件类型
	 * @param int $id 当前表单模型的id(可选参数)
	 * @return string
	 */
	public static function widget_type_change($type='',$id=0){
		if(!My::is_var($type)) return false;
		$id = My::is_id($id)?$id:'';
		$result = array();
		if(My::is_id($id)){
			$result = DB('form')->where("id={$id}")->find();
			//exit($result['params']);
			$result['params'] = My::decodeJson($result['params'],true);
			//print_r($result['params']);exit;
		}
		$widget_type = Form::getType();
		if(empty($widget_type[$type])) return false;
		//单项选择(radio)
		if($type=='radio' OR $type=='checkbox'){
			//select框的options选择
			$html = Widget::input_array2(array('name'=>'param[params][options]','data'=>My::arr2var($result,'params.options')));
		}
		//图片(img)
		elseif($type=='img'){
			//附加条件(如:限制打开的相册的table/idid等)
			//$html =  "<p><p style='line-height:27px;text-align:right;width:60px;float:left;'>相册限制:</p><input style='width:132px !important;' placeholder='table=datas&idid={\$id}' type='text' name='param[params][photo_limit]' value='{$result['params']['photo_limit']}'/></p>";
		}
		//textarea
		elseif($type=='textarea'){
			$html = "<p><label class='valign'>width <input class='input-small' type='text' name='param[params][width]' placeholder='数字...' value='".(My::arr2var($result,'params.width'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>height <input class='input-small' type='text' name='param[params][height]' placeholder='数字...' value='".(My::arr2var($result,'params.height'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>maxlength <input class='input-small' type='text' name='param[params][maxlength]' placeholder='数字...' value='".(My::arr2var($result,'params.maxlength'))."'/></label></p>";
			$html.= "<p><label class='valign'>placeholder <input class='input-small' type='text' name='param[params][placeholder]' placeholder='请输入...' value='".(My::arr2var($result,'params.placeholder'))."'/></label></p>";
		}
		//html代码
		elseif($type=='html'){
			$html = "<p><label class='valign'>代码 <textarea name='param[params][html]' placeholder='请输入html代码...'>".(My::arr2var($result,'params.html'))."</textarea> </label></p>";
		}
		//input
		elseif($type=='input'){
			$html = "<p><label class='valign'>width <input class='input-small' type='text' name='param[params][width]' placeholder='数字...' value='".(My::arr2var($result,'params.width'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>maxlength <input class='input-small' type='text' name='param[params][maxlength]' placeholder='数字...' value='".(My::arr2var($result,'params.maxlength'))."'/></label></p>";
			$html.= "<p><label class='valign'>placeholder <input class='input-small' type='text' name='param[params][placeholder]' placeholder='请输入...' value='".(My::arr2var($result,'params.placeholder'))."'/></label></p>";
		}
		//input_date 日期控件
		elseif($type=='input_date'){
			$html = "<p><label class='valign'>类型 ".Widget::select(array('name'=>'param[params][kind]','option'=>array('datetime'=>'年月日时分秒','date'=>'年月日','yearmonth'=>'年月','monthday'=>'月日','year'=>'年','month'=>'月','day'=>'日','time'=>'时分秒'),'data'=>(My::arr2var($result,'params.kind'))))." </label></p>";
			$html.= "<p><label class='valign'>width <input class='input-small' type='text' name='param[params][width]' placeholder='数字...' value='".(My::arr2var($result,'params.width'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>placeholder <input class='input-small' type='text' name='param[params][placeholder]' placeholder='请输入...' value='".(My::arr2var($result,'params.placeholder'))."'/></label></p>";
		}
		//input_date 颜色选取控件
		elseif($type=='input_color'){
			$html = "<p class='red'>请记得在模板中包含颜色选取控件(colorpicker)</p>";
			$html.= "<p><label class='valign'>格式 ".Widget::select(array('name'=>'param[params][format]','option'=>array('hex'=>'16进度#模式','rgb'=>'普通模式','rgba'=>'透明模式'),'data'=>(My::arr2var($result,'params.format'))))." </label></p>";
			//$html.= "<p><label class='valign'>width <input class='input-small' type='text' name='param[params][width]' placeholder='数字...' value='".(My::arr2var($result,'params.width'))."'/> px</label></p>";
			//$html.= "<p><label class='valign'>placeholder <input class='input-small' type='text' name='param[params][placeholder]' placeholder='请输入...' value='".(My::arr2var($result,'params.placeholder'))."'/></label></p>";
		}
		//input_map 百度地图
		elseif($type=='input_map'){
			$html = "<p><label class='valign'>默认坐标：<input class='width150' type='text' name='param[params][point]' placeholder='坐标...' value='".(My::arr2var($result,'params.point'))."'/> 经,纬 </label></p>";
			$html.= "<p><label class='valign'>绽放级别：<input class='width150' type='text' name='param[params][level]' placeholder='默认缩放级别...' value='".(My::arr2var($result,'params.level'))."'/> 3-19 </label></p>";
			//$html.= "<p><label class='valign'>placeholder <input class='input-small' type='text' name='param[params][placeholder]' placeholder='请输入...' value='".(My::arr2var($result,'params.placeholder'))."'/></label></p>";
		}
		//input_array
		elseif($type=='input_array'){
			$html = "<p><label class='valign'>最大行数 <input class='input-small' type='text' name='param[params][max_num]' placeholder='数字...' value='".(My::arr2var($result,'params.max_num'))."'/> </label></p>";
			$html.= "<p><label class='valign'>width <input class='input-small' type='text' name='param[params][width]' placeholder='输入数字...' value='".(My::arr2var($result,'params.width'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>列名称 <input class='input-small' type='text' name='param[params][td_name]' placeholder='请输入...' value='".(My::arr2var($result,'params.td_name'))."'/></label></p>";
			$html.= "<p><label class='valign'>maxlength <input class='input-small' type='text' name='param[params][maxlength]' placeholder='输入数字...' value='".(My::arr2var($result,'params.maxlength'))."'/></label></p>";
			$html.= "<p><label class='valign'>placeholder <input class='input-small' type='text' name='param[params][placeholder]' placeholder='请输入...' value='".(My::arr2var($result,'params.placeholder'))."'/></label></p>";
		}
		//input_array2
		elseif($type=='input_array2'){
			$html = "<p><label class='valign'>最大行数 <input class='input-small' type='text' name='param[params][max_num]' placeholder='数字...' value='".(My::arr2var($result,'params.max_num'))."'/> </label></p>";
			$html.= "<p><label class='valign'>第1列名称 <input class='input-small' type='text' name='param[params][td_name1]' placeholder='请输入...' value='".(My::arr2var($result,'params.td_name1'))."'/></label></p>";
			$html.= "<p><label class='valign'>第1列宽度 <input class='input-small' type='text' name='param[params][td_width1]' placeholder='数字...' value='".(My::arr2var($result,'params.td_width1'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>maxlength1 <input class='input-small' type='text' name='param[params][td_maxlength1]' placeholder='数字...' value='".(My::arr2var($result,'params.td_maxlength1'))."'/></label></p>";
			$html.= "<p><label class='valign'>第2列名称 <input class='input-small' type='text' name='param[params][td_name2]' placeholder='请输入...' value='".(My::arr2var($result,'params.td_name2'))."'/></label></p>";
			$html.= "<p><label class='valign'>第2列宽度 <input class='input-small' type='text' name='param[params][td_width2]' placeholder='数字...' value='".(My::arr2var($result,'params.td_width2'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>maxlength2 <input class='input-small' type='text' name='param[params][td_maxlength2]' placeholder='数字...' value='".(My::arr2var($result,'params.td_maxlength2'))."'/></label></p>";
		}
		//(img[])动态图片+单个input
		elseif($type=='img_array2'){
			$html = "<p><label class='valign'>最大行数 <input class='input-small' type='text' name='param[params][max_num]' placeholder='数字...' value='".(My::arr2var($result,'params.max_num'))."'/> </label></p>";
			$html.= "<p><label class='valign'>文本框标题 <input class='input-small' type='text' name='param[params][input_title]' placeholder='请输入标题...' value='".(My::arr2var($result,'params.input_title'))."'/> </label></p>";
			$html.= "<p><label class='valign'>文本框宽度 <input class='input-small' type='text' name='param[params][input_width]' placeholder='数字...' value='".(My::arr2var($result,'params.input_width'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>文本框maxlength <input class='input-small' type='text' name='param[params][input_maxlength]' placeholder='数字...' value='".(My::arr2var($result,'params.input_maxlength'))."'/></label></p>";
			$html.= "<p><label class='valign'>图片框标题 <input class='input-small' type='text' name='param[params][img_title]' placeholder='请输入标题...' value='".(My::arr2var($result,'params.img_title'))."'/> </label></p>";
			$html.= "<p><label class='valign'>图片框宽度 <input class='input-small' type='text' name='param[params][img_width]' placeholder='数字...' value='".(My::arr2var($result,'params.img_width'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>图片框maxlength <input class='input-small' type='text' name='param[params][img_maxlength]' placeholder='数字...' value='".(My::arr2var($result,'params.img_maxlength'))."'/></label></p>";
		}
		//(img[])动态图片+单个input
		elseif($type=='img_array'){
			$html = "<p><label class='valign'>最大行数 <input class='input-small' type='text' name='param[params][max_num]' placeholder='数字...' value='".(My::arr2var($result,'params.max_num'))."'/> </label></p>";
		}
		//(img[])动态图片+两个input
		elseif($type=='img_array3'){
			$html = "<p><label class='valign'>最大行数 <input class='input-small' type='text' name='param[params][max_num]' placeholder='数字...' value='".(My::arr2var($result,'params.max_num'))."'/> </label></p>";
			$html.= "<p><label class='valign'>文本框标题1 <input class='input-small' type='text' name='param[params][input_title1]' placeholder='请输入标题...' value='".(My::arr2var($result,'params.input_title1'))."'/> </label></p>";
			$html.= "<p><label class='valign'>文本框宽度1 <input class='input-small' type='text' name='param[params][input_width1]' placeholder='数字...' value='".(My::arr2var($result,'params.input_width1'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>文本框maxlength1 <input class='input-small' type='text' name='param[params][input_maxlength1]' placeholder='数字...' value='".(My::arr2var($result,'params.input_maxlength1'))."'/></label></p>";
			$html.= "<p><label class='valign'>文本框标题2 <input class='input-small' type='text' name='param[params][input_title2]' placeholder='请输入标题...' value='".(My::arr2var($result,'params.input_title2'))."'/> </label></p>";
			$html.= "<p><label class='valign'>文本框宽度2 <input class='input-small' type='text' name='param[params][input_width2]' placeholder='数字...' value='".(My::arr2var($result,'params.input_width2'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>文本框maxlength2 <input class='input-small' type='text' name='param[params][input_maxlength2]' placeholder='数字...' value='".(My::arr2var($result,'params.input_maxlength2'))."'/></label></p>";
			$html.= "<p><label class='valign'>图片框标题 <input class='input-small' type='text' name='param[params][img_title]' placeholder='请输入标题...' value='".(My::arr2var($result,'params.img_title'))."'/> </label></p>";
			$html.= "<p><label class='valign'>图片框宽度 <input class='input-small' type='text' name='param[params][img_width]' placeholder='数字...' value='".(My::arr2var($result,'params.img_width'))."'/> px</label></p>";
			$html.= "<p><label class='valign'>图片框maxlength <input class='input-small' type='text' name='param[params][img_maxlength]' placeholder='数字...' value='".(My::arr2var($result,'params.img_maxlength'))."'/></label></p>";
		}
		//下拉选择(select)
		elseif($type=='select'){
			//print_r($result['params']);
			//是否使用chosen
			$selected = (isset($result['params']['is_chosen']) AND $result['params']['is_chosen']==1)?"checked='checked'":'';
			$html = "<p><label><input style='margin-top:0px;' {$selected} type='checkbox' name='param[params][is_chosen]' value='1'/>是否使用Chosen</label></p>";
			//是否multiple模式
			$selected = (isset($result['params']['is_multiple']) AND $result['params']['is_multiple']==1)?"checked='checked'":'';
			$html .= "<p><label class='valign'><input style='margin-top:0px;' {$selected} type='checkbox' name='param[params][is_multiple]' value='1'/>Multiple</label></p>";
			//宽度
			$html .= "<p><label class='valign'>宽度<input class='input-mini' type='text' name='param[params][width]' placeholder='200px' value='".(My::arr2var($result,'params.width'))."'/></label></p>";
			//select框的options选择
			$html .= Widget::input_array2(array('name'=>'param[params][options]','data'=>My::arr2var($result,'params.options')));
		}
		//数据表下拉(select)
		elseif($type=='select_db'){
			//是否使用chosen
			$selected = (isset($result['params']['is_chosen']) AND $result['params']['is_chosen']==1)?"checked='checked'":'';
			$html = "<p style='clear:both;'><label><input style='margin-top:0px;' {$selected} type='checkbox' name='param[params][is_chosen]' value='1'/>是否使用Chosen</label></p>";
			//selectDB下拉框
			$html .= '<p style="clear:both;"><p style="line-height:27px;text-align:right;width:40px;float:left;">数据表:</p>'.Form::select_db(array('name'=>'param[params][select_db_table]','type'=>'select_db_table','selected'=>My::arr2var($result,'params.select_db_table'))).'</p>';
			//数据表对应的字段select
			$html .= "<p style='clear:both;'><p style='line-height:27px;text-align:right;width:40px;float:left;'>key:</p><span class='param_params_select_db_key'>".Form::select_db(array('name'=>'param[params][select_db_key]','table'=>My::arr2var($result,'params.select_db_table'),'type'=>'select_db_fields','id'=>My::arr2var($result,'params.id')))."</span></p>";
			$html .= "<p style='clear:both;'><p style='line-height:27px;text-align:right;width:40px;float:left;'>value:</p><span class='param_params_select_db_value'>".Form::select_db(array('name'=>'param[params][select_db_value]','table'=>(My::arr2var($result,'params.select_db_table')),'type'=>'select_db_fields','id'=>(My::arr2var($result,'params.id'))))."</span></p>";
			//where条件
			$select_db_where = (My::arr2var($result,'params.select_db_where'));
			$html .=  "<p style='clear:both;'><p style='line-height:27px;text-align:right;width:40px;float:left;'>where:</p><input style='width:140px !important;' type='text' name='param[params][select_db_where]' value='{$select_db_where}'/></p>";
		}
		//selectFun下拉选择,函数接口提供的数据
		elseif($type=='select_fun'){
			//是否使用chosen
			$selected = (isset($result['params']['is_chosen']) AND $result['params']['is_chosen']==1)?"checked='checked'":'';
			$html = "<p style='clear:both;'><label><input style='margin-top:0px;' {$selected} type='checkbox' name='param[params][is_chosen]' value='1'/>是否使用Chosen</label></p>";
			$html .= "接口函数:<input style='width:140px;' placeholder='M(\"datas\")->myfun();' name='param[params][select_fun]' value='".(My::arr2var($result,'params.select_fun'))."'/>";
			//是否multiple模式
			$selected = (isset($result['params']['is_multiple']) AND $result['params']['is_multiple']==1)?"checked='checked'":'';
			$html .= "<p><label class='valign'><input style='margin-top:0px;' {$selected} type='checkbox' name='param[params][is_multiple]' value='1'/>Multiple</label></p>";
			//宽度
			$html .= "<p><label class='valign'>宽度<input class='input-mini' type='text' name='param[params][width]' placeholder='200px' value='".(My::arr2var($result,'params.width'))."'/></label></p>";
			//placeholder
			$html .= "<p><label class='valign'>placeholder<input class='input-small' type='text' name='param[params][placeholder]' value='".(My::arr2var($result,'params.placeholder'))."'/></label></p>";
		}
		//自定义插件包(package)
		elseif($type=='package'){
			//插件名(app1:package/app1.php)
			//$html  = "<p><p style='line-height:27px;text-align:right;width:60px;float:left;'><span>插件名:</span></p><input placeholder='如:app1' style='width:189px' type='text' name='param[params][appName]' value='".(My::arr2var($result,'params.appName'))."'/></p>";
			//对象A
			//$html .= "<p><p style='line-height:27px;text-align:right;width:60px;float:left;'><span>objA:</span></p><input placeholder='jq选择器' style='width:189px' type='text' name='param[params][objA]' value='".(My::arr2var($result,'params.objA'))."'/></p>";
			//对象B
			//$html .= "<p><p style='line-height:27px;text-align:right;width:60px;float:left;'><span>objB:</span></p><input placeholder='jq选择器' style='width:189px' type='text' name='param[params][objB]' value='".(My::arr2var($result,'params.objB'))."'/></p>";
			//相册附加条件(如:限制打开的相册的table/idid等)
			//$html .=  "<p><p style='line-height:27px;text-align:right;width:60px;float:left;'><span title='table=datas&idid={\$id}'>相册限制:</span></p><input style='width:189px' placeholder='table=datas&idid={\$id}' type='text' name='param[params][photo_limit]' value='".(My::arr2var($result,'params.photo_limit'))."'/></p>";
		}
		//混合自定义(more)
		elseif($type=='more'){
			//select框的options选择
			$html = Widget::__more(array('name'=>'param[params][options]','data'=>My::arr2var($result,'params.options')));
		}
		//API自定义HTML
		elseif($type=='html_api'){
			$html = "接口函数:<input style='width:140px;' placeholder='M(\"datas\")->myfun();' name='param[params][api]' value='".(My::arr2var($result,'params.api'))."'/>";
		}
		return isset($html)?$html:'';
	}

	/**
	 * 获得白名单字段(一唯数组)
	 * @param string/int 分类id 或 默认表单模型唯一名称item
	 */
	public static function getWhite($param=''){
		//得到分类id
		if(My::is_id($param))
			$cat_id = $param;
		elseif(My::is_item($param))
			$cat_id = M('admin.category')->item2id($param);
		else
			return false;
		//当前表单的验证规则(读取缓存文件)<三大类数据：rule:表单验证规则 / white:白名单字段 / indexing:索引字段>
		$result = Form::gretVerifyRule($cat_id);
		//白名单
		return isset($result['white'])?$result['white']:'';
	}
	/**
	 * 获得索引字段(一唯数组)
	 * @param string/int 分类id 或 默认表单模型唯一名称item
	 */
	public static function getIndexing($param=''){
		//得到分类id
		if(My::is_id($param))
			$cat_id = $param;
		elseif(My::is_item($param))
			$cat_id = M('admin.category')->item2id($param);
		else
			return false;
		//当前表单的验证规则(读取缓存文件)<三大类数据：rule:表单验证规则 / white:白名单字段 / indexing:索引字段>
		$result = Form::gretVerifyRule($cat_id);
		//索引字段(一唯数组)
		return isset($result['indexing'])?$result['indexing']:'';
	}
	/**
	 * 开始验证
	 * @param string $param (分类id) //分类ID:相应分类ID下的指向表单模型 (两参数二选一)
	 */
	public static function verify($cat_id=0){
		if(!My::is_id($cat_id)) return false;
		//得到验证类型
		//$verify_type = Form::getVerifyType();
		//当前表单的验证规则(读取缓存文件)<三大类数据：rule:表单验证规则 / white:白名单字段 / indexing:索引字段>
		$result = Form::gretVerifyRule($cat_id);
		//循环规则
		if(isset($result['rule']) AND is_array($result['rule']))
		foreach($result['rule'] as $key=>$value){
			//确定表单控件的名称
			if(substr($key,0,7)=='params[') $varName = "\$varValue=\$_POST['params']".str_replace('params','',$key).';';
			else $varName = "\$varValue=\$_POST['{$key}'];";
			@eval($varName);
			if(isset($varValue)){//是否存在该post变量
				//echo $varValue;exit;
				if(is_array($value['function']))
				foreach($value['function'] as $k=>$v)
				if(($varValue!='' OR $v=='isRequired') AND !Validator::$v($varValue)){//如果为空就不验证了-直接返回(除非当前验证函数是isRequired) AND 执行验证函数(返回boolean结果)
					$widget_type = (strpos($value['widget_type'][$k],'_')!==FALSE)?str_replace(strstr($value['widget_type'][$k],'_'),'',$value['widget_type'][$k]):$value['widget_type'][$k];//strstr($value,"-",true);
					return array('widget_type'=>$widget_type,'field_name'=>$key,'message'=>$value['message'][$k]);
				}
			}
		}
		return true;
	}
	/**
	 * 生成验证规则
	 * @param string $name 要验证的表单名称
	 */
	private static function gretVerifyRule($cat_id=''){
		//读取缓存文件
		$result = F('Cache/verify_'.$cat_id);
		//验证文件不存在时
		if(!$result){
			//得到验证类型
			$verify_type = Form::getVerifyType();
			//ORM字段
			$temp_ = DB('datas')->getDbFields();
			if(is_array($temp_))
			foreach($temp_ as $value){
				$field[$value] = $value;
			}
			//循环生成验证函数
			$list = DB('form')->where("cat_id='{$cat_id}' AND publish=1")->order('ordering ASC')->cache()->select();
			//$list = My::customSort($list,$field,'field_name');//自定义对数组进行排序(数组,自定义顺序数组,字段)
			if(is_array($list))
			foreach($list as $key=>$value){
				$verify_arr = My::decodeJson($value['verify_rule']);
				$value['params'] = My::decodeJson($value['params']);
				//是否:白名单
				if(isset($value['params']['white']) AND $value['params']['white']=='1')
					$result['white'][] = $value['field_name'];
				//是否:索引
				if(isset($value['params']['indexing']) AND $value['params']['indexing']=='1')
					$result['indexing'][] = $value['field_name'];
				//要验证的类型
				if(!empty($value['verify_rule'])){
					//有验证规则时
					if(is_array($verify_arr))
					foreach($verify_arr as $val){
						//确定控件名称(自定义控件要加上params)
						if($value['is_custom']==1) $field_name = "params[{$value['field_name']}]";
						else $field_name = $value['field_name'];
						//函数
						$result['rule'][$field_name]['function'][] = $verify_type[$val]['function'];
						//标题
						$result['rule'][$field_name]['message'][] = ($value['title']?$value['title']:$value['field_name']).$verify_type[$val]['message'];
						//控件类型
						$result['rule'][$field_name]['widget_type'][] = $value['widget_type'];
					}
				}
			}
			F('verify_'.$cat_id, $result, DATA_DIR.'Cache'.__DS__);
			//print_r($result);exit;
		}
		return $result;
	}
	
	/**
	 * selectDB下拉框(针对数据库的数据)
	 */
	public static function select_db($params){
		switch($params['type']){
			case 'select_db_table':
				$dbs = DB('')->tables();
				$html = "<select name='{$params['name']}'>";
				$html.= "<option value=''>请选择数据表...</option>";
				if(is_array($dbs))
				foreach($dbs as $value){
					if($value==$params['selected']) $selected="selected='selected'"; else $selected="";
					$html .= "<option {$selected} value='{$value}'>{$value}</option>";
				}
				$html .= '</select>';
			break;
			case 'select_db_fields':
				$table = str_replace(C('DB_PREFIX'),'',$params['table']);
				if($table=='') return false;
				$result = DB($table)->getDbFields();
				$html = '';
				//有id值时,得到是否有select选中相关参数
				if(My::is_id($params['id'])){
					$temp = DB('form')->where("id={$params['id']}")->find();
					$temp['params'] = !empty($temp['params'])?My::decodeJson($temp['params']):'';
				}
				//组装select代码
				if(is_array($result))
				foreach($result as $value){
					//字段name=param[params][select_db_key] AND 字段名相同
					if($params['name']=='param[params][select_db_key]' AND isset($temp['params']['select_db_key']) AND $value==$temp['params']['select_db_key'])
						$selected="selected='selected'"; else $selected="";
					//字段name=param[params][select_db_value] AND 字段名相同
					if($params['name']=='param[params][select_db_value]' AND isset($temp['params']['select_db_value']) AND $value==$temp['params']['select_db_value'])
						$selected="selected='selected'"; else $selected="";
					$html .= "<option {$selected} value='{$value}'>{$value}</option>";
				}
				$html = "<select name='{$params['name']}'>{$html}</select>";
			break;
			default:
				$html = '';
		}
		return $html;
	}
	
	/**
	 * 获得指定表单中的字段信息
	 * @param string $item 表单模型的item字段
	 * @param string $field_name 表单模型的指定字段名称
	 * @return array | string
	 */
	public static function get_form_field($item,$field_name){
		$result = '';
		if($cat = DB('category')->where("`item`='{$item}'")->find()){
			//查询并返回字段的信息
			if($data = DB('form')->where("`cat_id`={$cat['id']} AND `field_name`='{$field_name}'")->find()){
				$params = $data['params'] = My::decodeJson($data['params']);
				//判断参数的类型
				if($data['widget_type'] == 'select_db'){
					$sql = 'SELECT * FROM '.$params['select_db_table'].' WHERE '.$params['select_db_where'];
					if($res = DB('')->query($sql)){
						foreach ($res as $row){
							$data['params']['options'][$row[$params['select_db_key']]] = $row[$params['select_db_value']];
						}
						$result = $data['params']['options'];
					}
				}elseif($data['widget_type'] == 'select' || $data['widget_type'] == 'radio'){
					$result = $data['params']['options'];
				}else{
					$result = $data['params'];
				}
			}
		}
		return $result;
	}

}